using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace Oops.Xna.Framework.UI
{
	public class ControlCollection : KeyedGameComponentCollection<string, Control>
	{
		private Control parent;

		public ControlCollection(Control parent)
		{
			// Hold onto parameters.
			this.parent = parent;
		}

		#region KeyedCollection Members

		protected override string GetKeyForItem(Control item)
		{
			return item.Name;
		}

		#endregion

		#region Collection Members

		protected override void InsertItem(int index, Control item)
		{
			// Call inherited method.
			base.InsertItem(index, item);

			// Associate the rigid body to the collision shape.
			item.Parent = this.parent;
		}

		protected override void RemoveItem(int index)
		{
			Control item = this[index];

			// Call inherited method.
			base.RemoveItem(index);

			// Unassociated the parent from the control.
			if (item != null)
			{
				item.Parent = null;
			}
		}

		protected override void ClearItems()
		{
			// Unassign all parent's associations with the items.
			for (int i = 0; i < this.Count; i++)
			{
				this.Items[i].Parent = null;
			}

			// Call inherited method.
			base.ClearItems();
		}

		#endregion
	}
}
